package com.deer.wms.ware.task.model.pushErp.enter;

import cn.hutool.core.bean.OptionalBean;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.model.Area.AreaInfo;
import com.deer.wms.base.system.model.Item.ItemInfo;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.base.system.service.AreaInfoService;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.BeanUtils;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.enums.SoTypeResolveEnum;
import com.deer.wms.ware.task.model.out.OutDetail;
import com.deer.wms.ware.task.model.pushErp.PushErpBean;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PushErpService;
import com.deer.wms.ware.task.service.SoMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: xu
 * @Date: 2022/06/17/13:03
 * @Description:
 */
@Component
public class NewAlloteBean implements PushErpBean {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private PushErpService pushErpService;
    @Autowired
    private ItemInfoService itemInfoService;
    @Autowired
    private SoMasterService soMasterService;

    @Autowired
    private AreaInfoService areaInfoService;
    @Autowired
    CellInfoService cellInfoService;
    @Autowired
    private InventoryService inventoryService;
    @Override
    public Integer gainFileType() {
       return SoTypeResolveEnum.NEWALLOT.getStatenum();
    }

    @Override
    public Map<String, String> getBeanAndPush(SoMaster soMaster, List<OutDetail> outDetails, BaseAssist enterAssist) {

        OrderToErpBO orderToErpBO = new OrderToErpBO();
        Integer customerId=  soMasterService.findCustomerId(soMaster.getBillNo());
        Integer toErpId = soMasterService.findtoErpId(soMaster.getBillNo());
        //客户
        //发出商品仓库
        AreaInfo areaInfo = areaInfoService.getOne(new QueryWrapper<AreaInfo>().eq("erp_area_id", toErpId));
        Integer waveType = areaInfo.getWaveType();
        if (2 != OptionalBean.ofNullable(waveType).orElse(0)) {
            return null;
        }
        orderToErpBO.setBillTypeId(41);
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("OUT"));
        orderToErpBO.setBillDate(DateUtils.getNowDateString());
        orderToErpBO.setCustomerId(customerId);
        orderToErpBO.setCategoryId(ObjectUtil.isNotEmpty(enterAssist.getCategoryId()) ? enterAssist.getCategoryId() : 41146); //默认其他调拨
        List<OrderDetailBO> details = new ArrayList<>();
        outDetails.forEach(item -> {
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            Inventory oldInventory = inventoryService.getById(item.getInvId());
//            if (ObjectUtil.isNotEmpty(oldInventory)) {
//                Inventory inventory = new Inventory();
//                BeanUtils.copyProperties(oldInventory, inventory);
//                oldInventory.setAllotQuantity(0.00);
//                inventory.setInventoryId(null);
//                inventory.setCellCode(cellInfoService.findErpAreaId(toErpId));
//                inventory.setBoxCode(codeRuleService.generateCodeByRedis("CT"));
//                inventory.setErpAreaId(toErpId);
//                inventory.setQuantity(oldInventory.getQuantity());
//                inventory.setCellCode(codeRuleService.generateCellCodeRedis());
//                inventory.setBillNo(null);
//                inventoryService.save(inventory);
//            }
            //新增库存
            orderDetailBO.setSourceBillId(item.getErpBillId());//源单据id
            orderDetailBO.setSourceBillDetailRowIndex(item.getBomSplit());//行号
            orderDetailBO.setQty(item.getDeliveryQuantity());//数量
            orderDetailBO.setBatchNo(item.getBatchName()); //计划跟踪号
            orderDetailBO.setStockId(item.getErpAreaId()); //调出仓库
            orderDetailBO.setMtoNo(item.getMtoNo());
            orderDetailBO.setAgreementNo(item.getMemo());
            ItemInfo itemInfo = itemInfoService.getOne(new QueryWrapper<ItemInfo>().eq("item_code", item.getItemCode()));
            orderDetailBO.setItemId(itemInfo.getErpItemId());
            orderDetailBO.setStockId2(toErpId); //调入仓库
            details.add(orderDetailBO);
        });
        orderToErpBO.setDetails(details);
        return  pushErpService.CreateIMBill(orderToErpBO);
    }
}
